<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="layui-row">
    <div class="layui-col-xs3">
        <div>
            <div id="test1" class="demo-tree demo-tree-box"></div>
        </div>
    </div>
    <div class="layui-col-xs9">
        <table class="layui-hide" id="test" lay-filter="test"></table>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="getCheckData">添加</button>
            </div>
        </script>
    </div>
</div>



<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="/layuiadmin/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table','tree'], function(){
        var table = layui.table;
        let tree=layui.tree;
        //模拟数据1
        let $=layui.jquery;
        let data='';
        let listData='';
        let fatherId=''
        $.ajax({
            url:"/permission/list",
            method:"POST",
            dataType: "json",
            success:function(res){
                data= res
                //无连接线风格
                tree.render({
                    elem: '#test1'
                    ,data: res
                    // ,showLine: false  //是否开启连接线
                    ,click: function(item){ //点击节点回调
                        fatherId=item.data.id
                        console.log(item.data.id)
                        $.ajax({
                            url:"/permission/listData",
                            method:"POST",
                            data:{'id':item.data.id},
                            dataType: "json",
                            success:function(res){
                                listData=res
                                table.render({
                                    elem: '#test'
                                    ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                                    ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                                        title: '提示'
                                        ,layEvent: 'LAYTABLE_TIPS'
                                        ,icon: 'layui-icon-tips'
                                    }]
                                    ,title: '用户数据表'
                                    ,data:listData.records
                                    ,cols: [
                                        [
                                            {type: 'checkbox', fixed: 'left'}
                                            ,{field:'id', title:'id', width:80, fixed: 'left',  sort: true}
                                            ,{field:'name', title:'名称', width:120, edit: 'text'}
                                            ,{field:'resourceType', title:'类型', width:150, edit: 'text'}
                                            ,{field:'url', title:'路径', width:80, edit: 'text', sort: true}
                                            ,{field:'perms', title:'权限', width:100}
                                            ,{field:'parentId', title:'父类'}
                                            ,{field:'type', title:'状态', width:80, sort: true}
                                            ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
                                        ]
                                    ]
                                    ,page: true
                                });
                            }
                        })
                    }
                });
            }
        })
        table.render({
            elem: '#test'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '用户数据表'
            ,cols: [
                [
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'id', width:80, fixed: 'left',  sort: true}
                ,{field:'id', title:'id', width:80, fixed: 'left',  sort: true}
                ,{field:'name', title:'名称', width:120, edit: 'text'}
                ,{field:'resourceType', title:'类型', width:150, edit: 'text'}
                ,{field:'url', title:'路径', width:80, edit: 'text', sort: true}
                ,{field:'perms', title:'权限', width:100}
                ,{field:'parentId', title:'父类'}
                ,{field:'type', title:'状态', width:80, sort: true}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]
            ]
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    layer.open({
                        type: 2
                        ,title: '添加菜单'
                        ,content: '/permission/inster?parentId='+fatherId
                        ,maxmin: true
                        ,area: ['700px', '500px']
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero) {
                            let formSubmit=layer.getChildFrame('form', index);
                            console.log(formSubmit)
                            let submited = formSubmit.find('#LAY-user-back-submit')[0];
                            console.log(submited)
                            submited.click();
                            layer.close(index); //关闭弹层
                            location.reload();
                        }
                    });
                    break;
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    // $.ajax({
                    //     url:"/permission/delete",
                    //     method:"POST",
                    //     data:{'id':data.id},
                    //     dataType: "json",
                    //     success:function(res){
                            obj.del();
                            layer.close(index);
                    //     }
                    // })
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '修改菜单'
                    ,content: '/permission/update?id='+data.id
                    ,maxmin: true
                    ,area: ['700px', '500px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero) {
                        let formSubmit=layer.getChildFrame('form', index);
                        console.log(formSubmit)
                        let submited = formSubmit.find('#LAY-user-back-submit')[0];
                        console.log(submited)
                        submited.click();
                        layer.close(index); //关闭弹层
                        location.reload();
                    }
                });
            }
        });
    });
</script>

</body>
</html>